using UnityEngine;

public class HitMark : MonoBehaviour
{
	public DamageManager DamageManage;

	public ItemSticker Armor;

	public GameObject HitFX;

	public float DamageMult = 1f;

	public int HitboxPhysicLayer = 8;

	private void Awake()
	{
		base.gameObject.layer = HitboxPhysicLayer;
	}

	private void Start()
	{
		if ((bool)base.transform.root)
		{
			DamageManage = base.transform.root.GetComponent<DamageManager>();
		}
		else
		{
			DamageManage = base.transform.GetComponent<DamageManager>();
		}
	}

	public void OnHit(DamagePackage pack)
	{
		if ((bool)DamageManage)
		{
			float num = 1f;
			if (Armor != null)
			{
				ItemEquipment equipped = Armor.GetEquipped();
				if (equipped != null)
				{
					num = 1f - equipped.Armor;
				}
			}
			float num2 = (int)(byte)((float)(int)pack.Damage * DamageMult * num);
			if (num2 > 255f)
			{
				num2 = 255f;
			}
			DamageManage.ApplyDamage((byte)num2, pack.Direction, pack.ID, pack.Team);
			if (UnitZ.gameManager != null && UnitZ.gameManager.PlayerNetID == pack.ID && UnitZ.playerManager.PlayingCharacter != null && UnitZ.playerManager.PlayingCharacter.inventory != null && UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment != null && (bool)UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment.GetComponent<Crosshair>())
			{
				UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment.GetComponent<Crosshair>().Hit();
			}
		}
		ParticleFX(pack.Position, pack.Normal);
	}

	public void OnHitTest(DamagePackage pack)
	{
		if ((bool)DamageManage && UnitZ.gameManager != null && UnitZ.gameManager.PlayerNetID == pack.ID && UnitZ.playerManager.PlayingCharacter != null && UnitZ.playerManager.PlayingCharacter.inventory != null && UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment != null && (bool)UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment.GetComponent<Crosshair>())
		{
			UnitZ.playerManager.PlayingCharacter.inventory.FPSEquipment.GetComponent<Crosshair>().Hit();
		}
		ParticleFX(pack.Position, pack.Normal);
	}

	public void ParticleFX(Vector3 position, Vector3 normal)
	{
		if ((bool)HitFX)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(HitFX, position, Quaternion.identity);
			gameObject.transform.forward = normal;
			UnityEngine.Object.Destroy(gameObject, 3f);
		}
	}
}
